/**
 * v-auth
 * 按钮权限指令
 */
import type { Directive, DirectiveBinding } from 'vue'
import { useMenuStore } from '@/stores/modules/menu'
import { useGlobalStore } from '@/stores/modules/global'

const auth: Directive = {
  mounted(el: HTMLElement, binding: DirectiveBinding) {
    const { value } = binding
    const menuStore = useMenuStore()
    const globalStore = useGlobalStore()
    const authButtons = menuStore.btnMap.get(globalStore.routePath) || []
    if (value instanceof Array && value.length) {
      const hasPermission = value.every((item) => authButtons.includes(item))
      if (!hasPermission) el.remove()
    } else {
      if (!authButtons.includes(value)) el.remove()
    }
  }
}

export default auth
